
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       notify_tone_alarm.c
  * @author     baiyang
  * @date       2022-5-28
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "notify.h"
#include "notify_tone_alarm.h"

#include <string.h>
#include <stdlib.h>

#include "hal_tone_alarm.h"

#include <common/time/gp_time.h>
#include <file_manager/file_manager.h>
#include <common/console/console.h>
#include <common/microbee.h>
/*-----------------------------------macro------------------------------------*/
#if CONFIG_HAL_BOARD != HAL_BOARD_SITL_WIN

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/
static void tonealarm_ctor(tone_alarm_t tone_alarm);
static bool init(notify_device_t notify_dev);
static void play_tune(notify_device_t notify_dev, const char *str);
static void update(notify_device_t notify_dev);
static void handle_play_tune(notify_device_t notify_dev, const mavlink_message_t *msg);
static void play_tone(tone_alarm_t tone_alarm, const uint8_t tone_index);
static void stop_cont_tone(tone_alarm_t tone_alarm);
static void check_cont_tone(tone_alarm_t tone_alarm);
static void tonealarm_timer_task(void *parameter);
static bool tonealarm_thread_create(tone_alarm_t tone_alarm, const char *name);
/*----------------------------------variable----------------------------------*/
// Tunes follow the syntax of the Microsoft GWBasic/QBasic PLAY
//   statement, with some exceptions and extensions.
// See https://firmware.ardupilot.org/Tools/ToneTester/
const struct NotifyTone _tones[] = {
#define AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK 0
    { "MFT200L4<<<B#A#2", false },
#define AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK 1
    { "MFT100L4>B#A#2P8B#A#2", false },
#define AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK 2
    { "MFT200L4<B#", false },
#define AP_NOTIFY_TONE_LOUD_NEU_FEEDBACK 3
    { "MFT100L4>B#", false },
#define AP_NOTIFY_TONE_QUIET_POS_FEEDBACK 4
    { "MFT200L4<A#B#", false },
#define AP_NOTIFY_TONE_LOUD_POS_FEEDBACK 5
    { "MFT100L4>A#B#", false },
#define AP_NOTIFY_TONE_LOUD_READY_OR_FINISHED 6
    { "MFT100L4>G#6A#6B#4", false },
#define AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED 7
    { "MFT200L4<G#6A#6B#4", false },
#define AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED 8
    { "MFT100L4>A#A#A#A#", false },
#define AP_NOTIFY_TONE_QUIET_ARMING_WARNING 9
    { "MNT75L1O2G", false },
#define AP_NOTIFY_TONE_LOUD_WP_COMPLETE 10
    { "MFT200L8G>C3", false },
#define AP_NOTIFY_TONE_LOUD_LAND_WARNING_CTS 11
    { "MBT200L2A-G-A-G-A-G-", true },
#define AP_NOTIFY_TONE_LOUD_VEHICLE_LOST_CTS 12
    { "MBT200>A#1", true },
#define AP_NOTIFY_TONE_LOUD_BATTERY_ALERT_CTS 13
    { "MBNT255>A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8A#8", true },
#define AP_NOTIFY_TONE_QUIET_CALIBRATING_CTS 14
    { "MBNT255<C16P2", true },
#define AP_NOTIFY_TONE_WAITING_FOR_THROW 15
    { "MBNT90L4O2A#O3DFN0N0N0", true},
#define AP_NOTIFY_TONE_LOUD_1 16
    { "MFT100L8>B", false},
#define AP_NOTIFY_TONE_LOUD_2 17
    { "MFT100L8>BB", false},
#define AP_NOTIFY_TONE_LOUD_3 18
    { "MFT100L8>BBB", false},
#define AP_NOTIFY_TONE_LOUD_4 19
    { "MFT100L8>BBBB", false},
#define AP_NOTIFY_TONE_LOUD_5 20
    { "MFT100L8>BBBBB", false},
#define AP_NOTIFY_TONE_LOUD_6 21
    { "MFT100L8>BBBBBB", false},
#define AP_NOTIFY_TONE_LOUD_7 22
    { "MFT100L8>BBBBBBB", false},
#define AP_NOTIFY_TONE_TUNING_START 23
    { "MFT100L20>C#D#", false},
#define AP_NOTIFY_TONE_TUNING_SAVE 24
    { "MFT100L10DBDB>", false},
#define AP_NOTIFY_TONE_TUNING_ERROR 25
    { "MFT100L10>BBBBBBBB", false},
#define AP_NOTIFY_TONE_LEAK_DETECTED 26
    { "MBT255L8>A+AA-", true},
#define AP_NOTIFY_TONE_QUIET_SHUTDOWN 27
    { "MFMST200L32O3ceP32cdP32ceP32c<c>c<cccP8L32>c>c<P32<c<c", false },
#define AP_NOTIFY_TONE_QUIET_NOT_READY_OR_NOT_FINISHED 28
    { "MFT200L4<B#4A#6G#6", false },
#define AP_NOTIFY_TONE_STARTUP 29
    { "MFT240L8O4aO5dcO4aO5dcO4aO5dcL16dcdcdcdc", false },
#define AP_NOTIFY_TONE_NO_SDCARD 30
    { "MNBGG", false },
#define AP_NOTIFY_TONE_EKF_ALERT 31
    { "MBNT255>A#8A#8A#8A#8P8A#8A#8A#8A#8P8A#8A#8A#8A#8P8A#8A#8A#8A#8", true },
};

static struct notify_device_ops tones_notify_dev_ops = {.NotifyDeviceDestructor = NULL,
                                                        .init                   = init,
                                                        .update                 = update,
                                                        .handle_led_control     = NULL,
                                                        .handle_play_tune       = handle_play_tune,
                                                        .play_tune              = play_tune,
                                                        .rgb_control            = NULL,
                                                        .rgb_set_id             = NULL};

static bool first_run = true;
static rt_thread_t thread_tonealarm;
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
notify_device_t tonealarm_probe()
{
    tone_alarm_t tone_alarm = (tone_alarm_t)rt_malloc(sizeof(struct tone_alarm));

    if (tone_alarm) {
        rt_memset(tone_alarm, 0, sizeof(struct tone_alarm));
        tonealarm_ctor(tone_alarm);

        return (notify_device_t)tone_alarm;
    }

    return NULL;
}

static void tonealarm_ctor(tone_alarm_t tone_alarm)
{
    tone_alarm->_tones = &_tones[0];
    tone_alarm->notify_dev.ops = &tones_notify_dev_ops;

    rt_mutex_init(&tone_alarm->_mutex, "tonealarm", RT_IPC_FLAG_FIFO);
}

static bool init(notify_device_t notify_dev)
{
    tone_alarm_t tone_alarm = (tone_alarm_t)notify_dev;

    if (notify_buzzer_enabled() == false) {
        return false;
    }
#if ((defined(HAL_PWM_ALARM) || defined(HAL_PWM_ALT_ALARM)) && CONFIG_HAL_BOARD == HAL_BOARD_RTTHREAD) || CONFIG_HAL_BOARD == HAL_BOARD_SITL_WIN
    if (!hal_tonealarm_init(notify_get_buzzer_types())) {
        return false;
    }
#endif

    // set initial boot states. This prevents us issuing a arming
    // warning in plane and rover on every boot
    tone_alarm->flags.armed = notify_flags.armed;
    tone_alarm->flags.failsafe_battery = notify_flags.failsafe_battery;
    tone_alarm->flags.pre_arm_check = 1;
    tone_alarm->_cont_tone_playing = -1;

    tonealarm_thread_create(tone_alarm, "alarm");

#if defined(HAVE_FILESYSTEM_SUPPORT)
    // if we don't have a SDcard then play a failure tone instead of
    // normal startup tone. This gives the user a chance to fix it
    // before they try to arm. We don't do this on Linux as Linux
    // flight controllers don't usually have removable storage
    if (!fs_file_exist(HAL_BOARD_STORAGE_DIRECTORY)) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_NO_SDCARD);
        return true;
    }
#endif

    play_tone(tone_alarm, AP_NOTIFY_TONE_STARTUP);

    return true;
}

static void play_tune(notify_device_t notify_dev, const char *str)
{
    tone_alarm_t tone_alarm = (tone_alarm_t)notify_dev;

    rt_mutex_take(&tone_alarm->_mutex, RT_WAITING_FOREVER);

    mmlplayer_stop(&tone_alarm->_mml_player);
    strncpy(tone_alarm->_tone_buf, str, MB_NOTIFY_TONEALARM_TONE_BUF_SIZE);
    tone_alarm->_tone_buf[MB_NOTIFY_TONEALARM_TONE_BUF_SIZE-1] = 0;
    mmlplayer_play(&tone_alarm->_mml_player, tone_alarm->_tone_buf);

    rt_mutex_release(&tone_alarm->_mutex);
}

// update - updates led according to timed_updated.  Should be called at 50Hz
static void update(notify_device_t notify_dev)
{
    tone_alarm_t tone_alarm = (tone_alarm_t)notify_dev;
#if 0
    if (first_run) {
        first_run = false;

#if defined(HAVE_FILESYSTEM_SUPPORT)
        // if we don't have a SDcard then play a failure tone instead of
        // normal startup tone. This gives the user a chance to fix it
        // before they try to arm. We don't do this on Linux as Linux
        // flight controllers don't usually have removable storage
        if (!fs_file_exist(HAL_BOARD_STORAGE_DIRECTORY)) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_NO_SDCARD);
        } else {
            play_tone(tone_alarm, AP_NOTIFY_TONE_STARTUP);
        }
#else
        play_tone(tone_alarm, AP_NOTIFY_TONE_STARTUP);
#endif
    }
#endif
    // exit if buzzer is not enabled
    if (notify_buzzer_enabled() == false) {
        return;
    }

    check_cont_tone(tone_alarm);

    if (notify_flags.powering_off) {
        if (!tone_alarm->flags.powering_off) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_SHUTDOWN);
        }
        tone_alarm->flags.powering_off = notify_flags.powering_off;
        return;
    }

    if (notify_flags.compass_cal_running != tone_alarm->flags.compass_cal_running) {
        if (notify_flags.compass_cal_running) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_CALIBRATING_CTS);
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
        } else {
            if (tone_alarm->_cont_tone_playing == AP_NOTIFY_TONE_QUIET_CALIBRATING_CTS) {
                stop_cont_tone(tone_alarm);
            }
        }
    }
    tone_alarm->flags.compass_cal_running = notify_flags.compass_cal_running;

    if (notify_events.compass_cal_canceled) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
        return;
    }

    if (notify_events.initiated_compass_cal) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
        return;
    }

    if (notify_events.compass_cal_saved ||
        notify_events.temp_cal_saved) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED);
        return;
    }

    if (notify_events.compass_cal_failed ||
        notify_events.temp_cal_failed) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
        return;
    }

    if (notify_events.initiated_temp_cal) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
        return;
    }

    if (notify_flags.temp_cal_running != tone_alarm->flags.temp_cal_running) {
        if (notify_flags.temp_cal_running) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_CALIBRATING_CTS);
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
        } else {
            if (tone_alarm->_cont_tone_playing == AP_NOTIFY_TONE_QUIET_CALIBRATING_CTS) {
                stop_cont_tone(tone_alarm);
            }
        }
    }
    tone_alarm->flags.temp_cal_running = notify_flags.temp_cal_running;
    
    // don't play other tones if cal is running
    if (notify_flags.compass_cal_running ||
        notify_flags.temp_cal_running) {
        return;
    }

    // notify the user when autotune or mission completes
    if (notify_flags.armed && (notify_events.autotune_complete || notify_events.mission_complete)) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_READY_OR_FINISHED);
    }

    //notify the user when autotune fails
    if (notify_flags.armed && (notify_events.autotune_failed)) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK);
    }

    // notify the user when a waypoint completes
    if (notify_events.waypoint_complete) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_WP_COMPLETE);
    }

    // notify the user when their mode change was successful
    if (notify_events.user_mode_change) {
        if (notify_flags.armed) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_NEU_FEEDBACK);
        } else {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
        }
    }

    // notify the user when their mode change failed
    if (notify_events.user_mode_change_failed) {
        if (notify_flags.armed) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_NEG_FEEDBACK);
        } else {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
        }
    }

    // failsafe initiated mode change
    if (notify_events.failsafe_mode_change) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED);
    }

    // notify the user when arming fails
    if (notify_events.arming_failed) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
    }

    // notify the user when RC contact is lost
    if (tone_alarm->flags.failsafe_radio != notify_flags.failsafe_radio) {
        tone_alarm->flags.failsafe_radio = notify_flags.failsafe_radio;
        if (tone_alarm->flags.failsafe_radio) {
            // armed case handled by events.failsafe_mode_change
            if (!notify_flags.armed) {
                play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
            }
        } else {
            if (notify_flags.armed) {
                play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_POS_FEEDBACK);
            } else {
                play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
            }
        }
    }

    // notify the user when GCS failsafe is triggered
    if (tone_alarm->flags.failsafe_gcs != notify_flags.failsafe_gcs) {
        tone_alarm->flags.failsafe_gcs = notify_flags.failsafe_gcs;
        if (tone_alarm->flags.failsafe_gcs) {
            // armed case handled by events.failsafe_mode_change
            if (!notify_flags.armed) {
                play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEG_FEEDBACK);
            }
        } else {
            if (notify_flags.armed) {
                play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_POS_FEEDBACK);
            } else {
                play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_POS_FEEDBACK);
            }
        }
    }

    // notify the user when pre_arm checks are passing
    if (tone_alarm->flags.pre_arm_check != notify_flags.pre_arm_check) {
        tone_alarm->flags.pre_arm_check = notify_flags.pre_arm_check;
        if (tone_alarm->flags.pre_arm_check) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_READY_OR_FINISHED);
            tone_alarm->_have_played_ready_tone = true;
        } else {
            // only play sad tone if we've ever played happy tone:
            if (tone_alarm->_have_played_ready_tone) {
                play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NOT_READY_OR_NOT_FINISHED);
            }
        }
    }

    // check if arming status has changed
    if (tone_alarm->flags.armed != notify_flags.armed) {
        tone_alarm->flags.armed = notify_flags.armed;
        if (tone_alarm->flags.armed) {
            // arming tune
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_ARMING_WARNING);
        } else {
            // disarming tune
            play_tone(tone_alarm, AP_NOTIFY_TONE_QUIET_NEU_FEEDBACK);
            if (!tone_alarm->flags.leak_detected) {
                stop_cont_tone(tone_alarm);
            }
        }
    }

    // check if battery status has changed
    if (tone_alarm->flags.failsafe_battery != notify_flags.failsafe_battery) {
        tone_alarm->flags.failsafe_battery = notify_flags.failsafe_battery;
        if (tone_alarm->flags.failsafe_battery) {
            // battery warning tune
            play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_BATTERY_ALERT_CTS);
        }
    }

    // check parachute release
    if (tone_alarm->flags.parachute_release != notify_flags.parachute_release) {
        tone_alarm->flags.parachute_release = notify_flags.parachute_release;
        if (tone_alarm->flags.parachute_release) {
            // parachute release warning tune
            play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_ATTENTION_NEEDED);
        }
    }

    // lost vehicle tone
    if (tone_alarm->flags.vehicle_lost != notify_flags.vehicle_lost) {
        tone_alarm->flags.vehicle_lost = notify_flags.vehicle_lost;
        if (tone_alarm->flags.vehicle_lost) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_VEHICLE_LOST_CTS);
        } else {
            stop_cont_tone(tone_alarm);
        }
    }

    // waiting to be thrown vehicle tone
    if (tone_alarm->flags.waiting_for_throw != notify_flags.waiting_for_throw) {
        tone_alarm->flags.waiting_for_throw = notify_flags.waiting_for_throw;
        if (tone_alarm->flags.waiting_for_throw) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_WAITING_FOR_THROW);
        } else {
            stop_cont_tone(tone_alarm);
        }
    }

    if (tone_alarm->flags.leak_detected != notify_flags.leak_detected) {
        tone_alarm->flags.leak_detected = notify_flags.leak_detected;
        if (tone_alarm->flags.leak_detected) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_LEAK_DETECTED);
        } else {
            stop_cont_tone(tone_alarm);
        }
    }

    if (notify_events.tune_started) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_TUNING_START);
        notify_events.tune_started = 0;
    }
    if (notify_events.tune_next) {
        // signify which parameter in the set is starting
        play_tone(tone_alarm, AP_NOTIFY_TONE_LOUD_1 + (notify_events.tune_next-1));
        notify_events.tune_next = 0;
    }
    if (notify_events.tune_save) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_TUNING_SAVE);
        notify_events.tune_save = 0;
    }
    if (notify_events.tune_error) {
        play_tone(tone_alarm, AP_NOTIFY_TONE_TUNING_ERROR);
        notify_events.tune_error = 0;
    }

    // notify the user when ekf failsafe is triggered
    if (tone_alarm->flags.failsafe_ekf != notify_flags.failsafe_ekf) {
        tone_alarm->flags.failsafe_ekf = notify_flags.failsafe_ekf;
        if (tone_alarm->flags.failsafe_ekf) {
            play_tone(tone_alarm, AP_NOTIFY_TONE_EKF_ALERT);
        }
    }
}

/*
 *  handle a PLAY_TUNE message
 */
static void handle_play_tune(notify_device_t notify_dev, const mavlink_message_t *msg)
{
    tone_alarm_t tone_alarm = (tone_alarm_t)notify_dev;

    // decode mavlink message
    mavlink_play_tune_t packet;

    mavlink_msg_play_tune_decode(msg, &packet);

    rt_mutex_take(&tone_alarm->_mutex, RT_WAITING_FOREVER);

    mmlplayer_stop(&tone_alarm->_mml_player);

    strncpy(tone_alarm->_tone_buf, packet.tune, MIN(sizeof(packet.tune), sizeof(tone_alarm->_tone_buf)-1));
    tone_alarm->_tone_buf[sizeof(tone_alarm->_tone_buf)-1] = 0;

    uint8_t len = strlen(tone_alarm->_tone_buf);
    uint8_t len2 = rt_strnlen(packet.tune2, sizeof(packet.tune2));
    len2 = MIN((sizeof(tone_alarm->_tone_buf)-1)-len, len2);
    strncpy(tone_alarm->_tone_buf+len, packet.tune2, len2);
    tone_alarm->_tone_buf[sizeof(tone_alarm->_tone_buf)-1] = 0;

    mmlplayer_play(&tone_alarm->_mml_player, tone_alarm->_tone_buf);

    rt_mutex_release(&tone_alarm->_mutex);
}

// play_tune - play one of the pre-defined tunes
static void play_tone(tone_alarm_t tone_alarm, const uint8_t tone_index)
{
    uint32_t tnow_ms = time_millis();
    const struct NotifyTone *tone_requested = &_tones[tone_index];

    if (tone_requested->continuous) {
        tone_alarm->_cont_tone_playing = tone_index;
    }

    tone_alarm->_tone_playing = tone_index;
    tone_alarm->_tone_beginning_ms = tnow_ms;

    notify_device_play_tune(&tone_alarm->notify_dev, tone_requested->str);
}

static void stop_cont_tone(tone_alarm_t tone_alarm)
{
    if (tone_alarm->_cont_tone_playing == tone_alarm->_tone_playing) {
        notify_device_play_tune(&tone_alarm->notify_dev, "");
        tone_alarm->_tone_playing = -1;
    }
    tone_alarm->_cont_tone_playing = -1;
}

static void check_cont_tone(tone_alarm_t tone_alarm)
{
    uint32_t tnow_ms = time_millis();
    // if we are supposed to be playing a continuous tone,
    // and it was interrupted, and the interrupting tone has timed out,
    // resume the continuous tone

    if (tone_alarm->_cont_tone_playing != -1 && tone_alarm->_tone_playing != tone_alarm->_cont_tone_playing && tnow_ms-tone_alarm->_tone_beginning_ms > MB_NOTIFY_TONEALARM_MAX_TONE_LENGTH_MS) {
        play_tone(tone_alarm, tone_alarm->_cont_tone_playing);
    }
}

/**
  * @brief       
  * @param[in]     
  * @param[out]  
  * @retval      
  * @note        1kHz
  */
static void tonealarm_timer_task(void *parameter)
{
    tone_alarm_t tone_alarm = (tone_alarm_t)parameter;

    while(1)
    {
        rt_mutex_take(&tone_alarm->_mutex, RT_WAITING_FOREVER);
        mmlplayer_update(&tone_alarm->_mml_player);
        rt_mutex_release(&tone_alarm->_mutex);

        rt_thread_mdelay(2);
    }
}

static bool tonealarm_thread_create(tone_alarm_t tone_alarm, const char *name)
{
    rt_err_t res = RT_ERROR;

    thread_tonealarm = rt_thread_create(name, tonealarm_timer_task, (void *)tone_alarm, 512, PRIORITY_IO, 1);

    if (thread_tonealarm != NULL) {
        res = rt_thread_startup(thread_tonealarm);
    }

    return res == RT_EOK;
}

/*------------------------------------test------------------------------------*/
#endif


